import { createFilterColumns, createTableRequestParams } from '@/utils/table';
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { PageContainer, ProTable } from '@ant-design/pro-components';
import { Button, Space, Typography } from 'antd';
import { useMemo, useRef, useState } from 'react';
import api from './request';
import WithdrawalForm from './WithdrawalForm';

function MerchantAccountMerchantUserWithdrawal() {
  const actionRef = useRef<ActionType>();

  const [filterList, setFilterList] = useState<Requestor.FilterResponse[]>([]);

  // request
  const getFilterList = async () => {
    // const filterList = await api.getFilterList();
    const filterList: Requestor.FilterResponse[] = [];
    setFilterList(filterList);
    return filterList;
  };
  const getListRequest = (
    params: Requestor.TableParams,
    filterList: Requestor.FilterResponse[],
  ) => {
    const requestParams = createTableRequestParams(params, filterList);
    return api.getList({ ...requestParams, sortField: 'withdrawalDate' });
  };
  const getList = async (params: Requestor.TableParams) => {
    if (!filterList || !filterList.length) {
      const filterList = await getFilterList();
      return getListRequest(params, filterList);
    } else {
      return getListRequest(params, filterList);
    }
  };

  const columns = useMemo(() => {
    const filterColumns =
      createFilterColumns<MerchantAccountMerchantUserWithdrawalApi.ListResponse>(filterList);
    const tableColumns: ProColumns<MerchantAccountMerchantUserWithdrawalApi.ListResponse>[] = [
      {
        title: '商家名称',
        dataIndex: 'merchantName',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '提现金额',
        dataIndex: 'withdrawalAmount',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '提现状态',
        dataIndex: 'statusName',
        hideInSearch: true,
        ellipsis: true,
        align: 'center',
      },
      {
        title: '银行账户名',
        dataIndex: 'bankAccountName',
        hideInSearch: true,
        ellipsis: true,
        align: 'center',
      },
      {
        title: '银行账户',
        dataIndex: 'bankAccount',
        hideInSearch: true,
        ellipsis: true,
        align: 'center',
      },
      {
        title: '支付方式',
        dataIndex: 'paymentMethodName',
        hideInSearch: true,
        ellipsis: true,
        align: 'center',
      },

      {
        title: '提现日期',
        dataIndex: 'withdrawalDate',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '提现备注',
        dataIndex: 'withdrawalRemark',
        hideInSearch: true,
        ellipsis: true,
        align: 'center',
      },
      {
        title: '审核备注',
        dataIndex: 'auditRemarks',
        hideInSearch: true,
        ellipsis: true,
        align: 'center',
      },
      {
        title: '操作',
        hideInSearch: true,
        align: 'center',
        width: '20%',
        render(_, record) {
          return (
            <Space>
              <Typography.Link>
                <WithdrawalForm data={record} onSuccess={() => actionRef.current?.reload()} />
              </Typography.Link>
            </Space>
          );
        },
      },
    ];
    return filterColumns.concat(tableColumns);
  }, [filterList]);
  return (
    <PageContainer>
      <ProTable
        bordered
        size="small"
        rowKey="id"
        options={false}
        actionRef={actionRef}
        pagination={{ defaultPageSize: 10, showSizeChanger: true }}
        toolBarRender={() => []}
        search={{
          optionRender: () => {
            return [
              <Button key="search" type="primary" onClick={() => actionRef.current?.reload()}>
                查询
              </Button>,
            ];
          },
        }}
        columns={columns}
        request={getList}
      />
    </PageContainer>
  );
}

export default MerchantAccountMerchantUserWithdrawal;
